package com.aaa.wash.service.impl;

import com.aaa.common.entity.FrontUserStore;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.ResultStatus;
import com.aaa.common.util.TokenUtil;
import com.aaa.common.vo.Page;
import com.aaa.wash.dao.FrontUserStoreDao;
import com.aaa.wash.service.FrontUserStoreService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * (FrontUserStore)表服务实现类
 *
 * @author makejava
 * @since 2025-10-09 20:09:35
 */
@Service("frontUserStoreService")
public class FrontUserStoreServiceImpl implements FrontUserStoreService {
    @Resource
    private FrontUserStoreDao frontUserStoreDao;
    @Resource
    private HttpServletRequest request;

    @Override
    public FrontUserStore selectByStoreIdAndUserId(Integer storeId) {
        String userId = TokenUtil.getUserId(request);
        return frontUserStoreDao.selectByStoreIdAndUserId(storeId, Integer.valueOf(userId));
    }

    @Override
    public FrontUserStore selectStoreNumber() {
        String userId = TokenUtil.getUserId(request);
        return frontUserStoreDao.selectStoreNumber(Integer.valueOf(userId));
    }

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public FrontUserStore queryById(Integer userId) {
        return this.frontUserStoreDao.queryById(userId);
    }

    /**
     * 分页查询
     *
     * @param page 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo queryByPage(Page<FrontUserStore> page) {
        //分页操作  分页参数一定要进行传递
        if (page == null || page.getPageNo() == null || page.getPageNo() == 0 || page.getPageSize() == null || page.getPageSize() == 0) {
            //进行业务编写时，可以抛出自定义异常
            throw new CustomException(ResultStatus.PARAM_NOT_EMPTY.getReturnCode(),
                    ResultStatus.PARAM_NOT_EMPTY.getReturnMessage());
        }
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        //获取查询对象
        FrontUserStore frontUserStore = page.getData();
        return new PageInfo(frontUserStoreDao.queryAllByLimit(frontUserStore));
    }

    /**
     * 新增数据
     *
     * @param
     * @return 实例对象
     */
    @Override
    public Integer insert(Integer storeId) {
        String userId = TokenUtil.getUserId(request);
        return this.frontUserStoreDao.insert(storeId, Integer.valueOf(userId));
    }

    @Override
    public Integer addUseToken(Integer userId, Integer storeId) {
        return this.frontUserStoreDao.insert(storeId, userId);
    }

    /**
     * 修改数据
     *
     * @param frontUserStore 实例对象
     * @return 实例对象
     */
    @Override
    public FrontUserStore update(FrontUserStore frontUserStore) {
        this.frontUserStoreDao.update(frontUserStore);
        return this.queryById(frontUserStore.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userId) {
        return this.frontUserStoreDao.deleteById(userId) > 0;
    }
}
